// 关于打印机配置的接口

import axios, { AxiosResponse } from 'axios';
import { tansParams } from './common';
import { ElNotification } from 'element-plus';
import printerCode from './printerCode';

export const printService = axios.create({
	baseURL: import.meta.env.VITE_PRINTER_BASE_API,
	// 超时
	timeout: 10000 * 20
});
printService.interceptors.request.use(
	(config: any) => {


		// Get请求映射params参数
		if (config.method === 'get' && config.params) {
			let url = config.url + '?' + tansParams(config.params);
			url = url.slice(0, -1);
			config.params = {};
			config.url = url;
			console.log('config.params:',config.params,'url:',url);
		}


		config.headers['Content-Type'] = 'application/json;charset=UTF-8';
		return config;
	},
	error => {
		console.log(error);
		Promise.reject(error);
	},
);
printService.interceptors.response.use((res: AxiosResponse) => {
	const {code,msg} = res.data;
	if(code===0){
		return res.data.data;
	}else{

		ElNotification.error({
			title: printerCode[code],
		});
		return Promise.reject('error');
	}
});
export default printService;